//===============================================================================
// Microsoft patterns & practices
// Mobile Client Software Factory - July 2006
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.Mobile.ConnectionMonitor;

namespace Microsoft.Practices.Mobile.DisconnectedAgent
{
	/// <summary>
	/// Adapter that implements the IConnectionMonitor interface required by the
	/// Disconnected Agent Block using the ConnectionMonitor block.
	/// </summary>
	public class ConnectionMonitorAdapter : IConnectionMonitor, IDisposable
	{
		bool disposed;
		ConnectionMonitor.ConnectionMonitor monitor;

		/// <summary>
		/// Fired when the ConnectionMonitor gets connection.
		/// </summary>
		public event EventHandler ConnectionStatusChanged;

		/// <summary>
		/// Constructor which creates a new adapter using the given ConnectionMonitor.
		/// </summary>
		/// <param name="monitor">ConnectionMonitor to be engaged.</param>
		public ConnectionMonitorAdapter(ConnectionMonitor.ConnectionMonitor monitor)
		{
			Guard.ArgumentNotNull(monitor, "monitor");
			this.monitor = monitor;
			monitor.ConnectionStatusChanged += ConnectionMonitor_ConnectionStatusChanged;
		}

		void ConnectionMonitor_ConnectionStatusChanged(object sender, EventArgs args)
		{
			if (ConnectionStatusChanged != null && monitor.IsConnected)
				ConnectionStatusChanged(sender, EventArgs.Empty);
		}

		/// <summary>
		/// This method gets the current connection price from the ConnectionMonitor.
		/// </summary>
		/// <exception cref="InvalidOperationException">
		/// If there is not active connection this method throws an InvalidOperationException.
		/// </exception>
		public uint CurrentConnectionPrice
		{
			get 
			{
				if (disposed) throw new ObjectDisposedException("ConnectionMonitorAdapter");
				Connection currentConnection = FindCurrentConnection();
				if (currentConnection == null)
				{
					throw new InvalidOperationException("You cannot get the current connection price if there is not active connection.");
				}
				return (uint) currentConnection.Price;
			}
		}

		/// <summary>
		/// Gets the current network name.
		/// If there is not active network returns null.
		/// </summary>
		public string CurrentNetwork
		{
			get 
			{
				if (disposed) throw new ObjectDisposedException("ConnectionMonitorAdapter");
				Connection currentConnection = FindCurrentConnection();
				return currentConnection != null ? currentConnection.GetNetworkName() : null;
			}
		}

		/// <summary>
		/// Gets the connection state.
		/// </summary>
		public bool IsConnected
		{
			get 
			{
				if (disposed) throw new ObjectDisposedException("ConnectionMonitorAdapter");
				return monitor.IsConnected; 
			}
		}

		/// <summary>
		/// This method disposes adapter used resources.
		/// </summary>
		public void Dispose()
		{
			if (disposed) throw new ObjectDisposedException("ConnectionMonitorAdapter");
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		private Connection FindCurrentConnection()
		{
			foreach (Connection c in monitor.ActiveConnections) return c;
			return null;
		}

		private void Dispose(bool disposing)
		{
			if (!this.disposed)
			{
				monitor.ConnectionStatusChanged -= ConnectionMonitor_ConnectionStatusChanged;
			}
			disposed = true;
		}

		~ConnectionMonitorAdapter()
		{
			Dispose(false);
		}
		
	}
}
